<?php
/**
 * 4、设计模式学习-建造者
 * 车辆控制类-车辆的表示层
 * 2019年12月5日15:14:55
 */
namespace Application\Controller;

use Application\Common\Builder\VehicleNoUseBuilder as VehicleNoUseBuilder;
use Application\Common\Builder\VehicleDirectorNoUseBuilder as VehicleDirectorNoUseBuilder;
use Application\Common\Builder\VehicleDirector as VehicleDirector;
use Application\Common\Builder\CarVehicle as CarVehicle;
use Application\Common\Builder\TruckVehicle as TruckVehicle;

class VehicleBuilderController extends BaseController
{
    // 第一种方法：构建车辆 没有使用 建造者设计模式
    public function noUseBuilder()
    {
        // 构建汽车
        $vehicle = new VehicleNoUseBuilder();
        $vehicle -> addWheel('4个');
        $vehicle -> addBody('承重式');
        $vehicle -> addEngine('汽油发动机');
        $vehicle_info = $vehicle -> display();
        echo '第一辆车的信息为：';
        echo '<br />';
        echo "车辆轮子：{$vehicle_info['wheel']}<br />";
        echo "车辆车身：{$vehicle_info['body']}<br />";
        echo "车辆引擎：{$vehicle_info['engine']}<br />";

        echo '<hr />';

        // 构建货车
        $vehicle2 = new VehicleNoUseBuilder();
        $vehicle2 -> addWheel('8个');
        $vehicle2 -> addBody('非承重式');
        $vehicle2 -> addEngine('柴油发动机');
        $vehicle2_info = $vehicle2 -> display();
        echo '第二辆车的信息为：';
        echo '<br />';
        echo "车辆轮子：{$vehicle2_info['wheel']}<br />";
        echo "车辆车身：{$vehicle2_info['body']}<br />";
        echo "车辆引擎：{$vehicle2_info['engine']}<br />";
        // 总结：构建与表示没有分离
    }
    // 第一种方法：构建车辆 没有使用 建造者设计模式 但使用了一个指挥类
    public function useDirectorNoUseBuilder()
    {
        // 构建汽车
        $vehicle_director = new VehicleDirectorNoUseBuilder(new VehicleNoUseBuilder);
        $car = array(
            'wheel' => '4个',
            'body' => '承重式',
            'engine' => '汽油发动机',
        );
        $vehicle_director -> createVehicle($car);
        $vehicle_info = $vehicle_director -> showVehicle();
        echo '第一辆车的信息为：';
        echo '<br />';
        echo "车辆轮子：{$vehicle_info['wheel']}<br />";
        echo "车辆车身：{$vehicle_info['body']}<br />";
        echo "车辆引擎：{$vehicle_info['engine']}<br />";

        echo '<hr />';

        // 构建卡车
        $vehicle_director = new VehicleDirectorNoUseBuilder(new VehicleNoUseBuilder);
        $truck = array(
            'wheel' => '8个',
            'body' => '非承重式',
            'engine' => '柴油发动机',
        );
        $vehicle_director -> createVehicle($truck);
        $vehicle_info = $vehicle_director -> showVehicle();
        echo '第二辆车的信息为：';
        echo '<br />';
        echo "车辆轮子：{$vehicle_info['wheel']}<br />";
        echo "车辆车身：{$vehicle_info['body']}<br />";
        echo "车辆引擎：{$vehicle_info['engine']}<br />";
        // 总结：构建与表示没有分离，不过类的构建较为容易
    }
    // 构建车辆 使用 建造者 设计模式
    public function useBuilder()
    {
        // 构建汽车
        $vehicle_director = new VehicleDirector(new CarVehicle());
        $vehicle_director -> createVehicle();
        $vehicle_info = $vehicle_director -> showVehicle();
        echo '第一辆车的信息为：';
        echo '<br />';
        echo "车辆轮子：{$vehicle_info['wheel']}<br />";
        echo "车辆车身：{$vehicle_info['body']}<br />";
        echo "车辆引擎：{$vehicle_info['engine']}<br />";

        echo '<hr />';
        // 构建卡车
        $vehicle_director = new VehicleDirector(new TruckVehicle());
        $vehicle_director -> createVehicle();
        $vehicle_info = $vehicle_director -> showVehicle();
        echo "第二辆车的信息为：<br />";
        echo "车辆轮子：{$vehicle_info['wheel']}<br />";
        echo "车辆车身：{$vehicle_info['body']}<br />";
        echo "车辆引擎：{$vehicle_info['engine']}<br />";
        // 总结：构建与表示分离，类的构建也较为容易
        // [关键]总结：建造者模式就是通过细化类的实例化对象，形成建造者基类 VehicleBuilder、建造者类 *Vehicle、建造者指挥者 VehicleDirector 来实现 负责对象的构建与它的表示分离
    }
}
